home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / ACL / CCL Folder / Examples / c.connections / Connections.cp next >
Encoding:
Text File  |  1994-08-31  |  4.3 KB  |  217 lines  |  [TEXT/MMCC]

  1. /********************************************
  2.  **** Core Class Demo by Yves Schmid
  3.  ****
  4.  **** Connections.cp
  5.  ****
  6.  **** Author:        Yves Schmid  
  7.  **** Created:      14 August 1994
  8.  **** Modified:     14 August 1994
  9.  **** Version:      1.0
  10.  **** Compatible:   C++
  11.  ****
  12.  **** Description:  Connections and duplication demo
  13.  ****
  14.  *************************/
  15.  
  16.  
  17. #include "CoreHead.h"
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. //*************************************
  23.  
  24.  
  25. const long cmd_printmyself    = 1024;    // A command which asks the stringnode to print its string
  26.  
  27.  
  28. //*************************************
  29. // A simple CoreHead which stocks and prints a string
  30.  
  31. class stringnode : public CoreHead
  32. {
  33.     char    data[256];
  34.  
  35.  
  36.     protected:
  37.  
  38.     // Override the "receivecmd" to handle the "cmd_printmyself" command:
  39.     void receivecmd(long cmd, void *info);
  40.  
  41.     public:
  42.  
  43.     inline char *getstring(void) {return data;}
  44.  
  45.     // Constructors
  46.  
  47.     stringnode(char *str, CoreHead *supervisor);    // Object with a supervisor
  48.     stringnode(char *str =NULL);                    // Unlinked object
  49.     
  50.     // Destructor
  51.     
  52.     ~stringnode();
  53.  
  54.     // Duplication system:
  55.     Core *duplicate(void);
  56.     void copyfrom(Core *);
  57. };
  58.  
  59. //..............................
  60.  
  61. stringnode::stringnode(char *str, CoreHead *supervisor) : CoreHead(supervisor,0,1)    
  62.                                             // Initialize CoreHead with the supervisor,
  63.                                             // destination entry 0 and 1 entry in the new
  64.                                             // created object 
  65.                                              
  66. {
  67.     newlevel();            // A new heritage level!
  68.     strcpy(data,str);    // Copy string
  69. }
  70.  
  71. //..............................
  72.  
  73.  
  74. stringnode::stringnode(char *str) : CoreHead(1)    // Unlinked object with one entry 
  75. {
  76.     newlevel();                    // A new heritage level!
  77.     
  78.     if (str) strcpy(data,str);    // Copy string
  79.     else str[0] = 0;            // else empty string
  80. }
  81.  
  82. //..............................
  83.  
  84. stringnode::~stringnode()
  85. {
  86.     printf("%s deleted\n", data);
  87. }
  88.  
  89. //..............................
  90.  
  91.  
  92. void stringnode::receivecmd(long cmd, void *info)
  93. {
  94.     switch(cmd)
  95.     {
  96.         case cmd_printmyself:    // Ok it is the right message, I can print the string.
  97.         printf("%s\n",data);    
  98.         break;
  99.         
  100.         default:                // Unknow message? Call the parent class
  101.         CoreHead::receivecmd(cmd,info);
  102.     }
  103. }
  104.  
  105. //..............................
  106.  
  107. Core *stringnode::duplicate(void)
  108. {
  109.     stringnode *no;
  110.  
  111.     clearerror();
  112.  
  113.     no = new stringnode();
  114.  
  115.     if (no)
  116.     {
  117.         no->copyfrom(this);
  118.         importerror(no);
  119.     }
  120.     else adderror(CERR_NOMEMORY);
  121.  
  122.     return no;
  123. }
  124.  
  125. void stringnode::copyfrom(Core *core)
  126. {
  127.  
  128.     CoreHead::copyfrom(core);
  129.  
  130.     if (core->getlevel()>=getlevel())
  131.     {
  132.         strcpy(data,((stringnode*)core)->getstring());
  133.         strcat(data,"-2");
  134.     }
  135. }
  136.  
  137. //..............................
  138.  
  139.  
  140. //*******************************************************************
  141.  
  142.  
  143. void main()
  144. {
  145.     // Creating a tree:
  146.     //
  147.     //          A
  148.     //        /   \
  149.     //       B1     B2
  150.     //      / \    / \
  151.     //     C1 C2 D1  D2
  152.  
  153.     printf("*** CCL Tree demo ***\n\n");
  154.     printf("Creating a tree:\n\n");
  155.     printf("         A            \n");
  156.     printf("       /   \\        \n");
  157.     printf("      B1    B2        \n");
  158.     printf("     /\\     /\\    \n");
  159.     printf("    C1 C2  D1 D2    \n");
  160.  
  161.     //*****************************
  162.  
  163.     stringnode *a,*b1,*b2,*c1,*c2,*d1,*d2,*tree2;
  164.  
  165.     a = new stringnode("A");
  166.     b1 = new stringnode("B1",a);
  167.     b2 = new stringnode("B2",a);
  168.     c1 = new stringnode("C1",b1);
  169.     c2 = new stringnode("C2",b1);
  170.     d1 = new stringnode("D1",b2);
  171.     d2 = new stringnode("D2",b2);
  172.  
  173.     //*****************************
  174.  
  175.     printf("\n\n* Duplicate \"B1\", result is a new tree:\n");
  176.     printf("      B1-2       \n");
  177.     printf("     /       \\   \n");
  178.     printf("    C1-2  C2-2  \n");
  179.  
  180.     tree2 = (stringnode*)b1->duplicate();
  181.     tree2->remove();    // Removes new tree from its supervisor which is "A"
  182.  
  183.     printf("\n\n* Scan the new tree:\n");
  184.     tree2->docmd(cmd_printmyself,CCF_EVERYWHERE);
  185.     
  186.  
  187.     //*****************************
  188.  
  189.     printf("\n\n* Connect the root of the second tree to \"B2\", new structure is:\n");
  190.     printf("         A            \n");
  191.     printf("       /   \\        \n");
  192.     printf("      B1    B2<----------B1-2       \n");
  193.     printf("     /\\     /\\          /  \\        \n");
  194.     printf("    C1 C2  D1 D2       C1-2  C2-2    \n");
  195.  
  196.     
  197.     tree2->connectto(b2);
  198.     
  199.     printf("\n\n* Scan the full structure using the root of the second tree (B1-2):\n");    
  200.     tree2->docmd(cmd_printmyself,CCF_EVERYWHERE);
  201.  
  202.  
  203.     //*****************************
  204.  
  205.  
  206.     printf("\n\n* Delete the \"B1\" node:\n");
  207.     delete b1;
  208.  
  209.     printf("\n\n* Delete the \"A\" node:\n");
  210.     delete a;    
  211.     
  212.     printf("\n\n* Delete the second tree:\n");
  213.     delete tree2;    
  214. }
  215.  
  216.  
  217.